home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -seriously_amiga- / gfxcard / cyberblanker / source-code / window.c < prev    next >
C/C++ Source or Header  |  1999-03-29  |  16KB  |  613 lines

  1.  
  2.    /***********************************************************************
  3.    *                                                                      *
  4.    *                            COPYRIGHTS                                *
  5.    *                                                                      *
  6.    *   Copyright (c) 1990  Commodore-Amiga, Inc.  All Rights Reserved.    *
  7.    *   This file was modified by © Zinneberg-Soft.                        *
  8.    ***********************************************************************/
  9.  
  10. /* window.c -- Intuition Interface */
  11.  
  12. #include "local.h"
  13.  
  14. #if WINDOW  /*##### All the following is disabled if the commodity #####*/
  15.             /*##### does not support a window.                     #####*/
  16.  
  17. struct Window   *window = NULL; /* our window */
  18.     struct TextAttr customtattr;
  19.     struct TextAttr *mydesiredfont;
  20.     struct TextFont *customfont = NULL;
  21. void  *vi  = NULL;
  22.      /*********************/
  23.       extern char *Windowy;
  24.       extern char *Windowx;
  25.       extern BOOL windowfirstopening;
  26.     /**********************/
  27.     extern char *fontname;
  28.     extern char *fontsize;
  29.     extern UBYTE  start_on_publicscreen[MAXPUBSCREENNAME];
  30.     struct Screen  *myscreen = NULL;
  31.  
  32. SHORT topborder;
  33. struct TextFont *font        = NULL;
  34. struct Gadget   *glist       = NULL;
  35. struct Menu     *menu        = NULL;
  36. struct DrawInfo *mydi        = NULL;
  37. BOOL            menuattached = NULL;
  38. BOOL            IDCMPRefresh = NULL;
  39.  
  40.         /* save window positions and dims left,top,width,height  */
  41. static WORD savewindow[ 4 ]={WINDOW_LEFT,WINDOW_TOP,WINDOW_WIDTH,WINDOW_HEIGHT};
  42.  
  43. static char WindowTitle[255];  /* buffer to hold cooked window title */
  44.  
  45. /****i* Blank.ld/handleIMsg() ******************************************
  46. *
  47. *   NAME
  48. *        handleIMsg -- Handle window IDCMP messages.
  49. *
  50. *   SYNOPSIS
  51. *        handleIMsg(msg);
  52. *
  53. *        VOID handleIMsg(struct IntuiMessage *msg);
  54. *
  55. *   FUNCTION
  56. *        This function handles all the IntuiMessages for standard
  57. *        commodities functions. If the message is for an application
  58. *        Gadget or Menu the appropriate application function,
  59. *        handleCustomMenu() or HandleGadget(), is called.
  60. *
  61. *   INPUTS
  62. *        msg = The current IntuiMessage.
  63. *
  64. *   RESULT
  65. *        The appropriate action for the message is performed.
  66. *
  67. *   EXAMPLE
  68. *
  69. *   NOTES
  70. *
  71. *   BUGS
  72. *
  73. *   SEE ALSO
  74. *
  75. *****************************************************************************
  76. *
  77. */
  78. VOID handleIMsg(struct IntuiMessage *msg)
  79. {
  80.    ULONG   class;
  81.    UWORD   code;
  82.    struct  Gadget *gaddress;
  83.  
  84.    class    = msg->Class;
  85.    code     = msg->Code;
  86.    gaddress = (struct Gadget *) msg->IAddress;
  87.  
  88.    D1( kprintf("window.c: handleIMsg() enter\n") );
  89.  
  90.    GT_ReplyIMsg( (struct IntuiMessage *) msg );
  91.  
  92.    switch ( class )
  93.    {
  94.       case CLOSEWINDOW:
  95.          D1( printf("window.c: handleIMsg(CLOSEWINDOW)\n") );
  96.          shutdownWindow();
  97.          break;         /* not reached  */
  98.       case REFRESHWINDOW:
  99.          D1( printf("window.c: handleIMsg(REFRESHWINODW)\n") );
  100.          IDCMPRefresh=TRUE;
  101.          refreshWindow();
  102.          IDCMPRefresh=FALSE;
  103.          break;
  104.       case MENUPICK:
  105.          D1( printf("window.c: handleIMsg(MENUPICK)\n") );
  106.          handleCustomMenu(code);
  107.          break;
  108.       case GADGETUP:
  109.          D1( printf("window.c: handleIMsg(GADGETUP) GadgetID=%lx\n",gaddress->GadgetID) );
  110.          HandleGadget(gaddress->GadgetID & GADTOOLMASK,code);
  111.          break;
  112.    }
  113. }
  114. /****i* Blank.ld/setupWindow() ******************************************
  115. *
  116. *   NAME
  117. *        setupWindow -- Perform whatever steps necessary to make the
  118. *                       window visible.
  119. *
  120. *   SYNOPSIS
  121. *        setupWindow()
  122. *
  123. *        VOID setupWindow(VOID);
  124. *
  125. *   FUNCTION
  126. *        This function is used to make the window visible. If the window
  127. *        is not opened this function will open it. If the window is already
  128. *        open it will be brought to the front so it is visible. This
  129. *        routine handles all the ugliness of new look and changing window
  130. *        title bar font heights.
  131. *
  132. *   INPUTS
  133. *
  134. *   RESULT
  135. *
  136. *   EXAMPLE
  137. *
  138. *   NOTES
  139. *
  140. *   BUGS
  141. *
  142. *   SEE ALSO
  143. *
  144. *****************************************************************************
  145. *
  146. */
  147. VOID setupWindow()
  148. {
  149.   
  150.    D1( printf("window.c: setupWindow() enter\n") );
  151.  
  152.    if(window)
  153.    {
  154.       D1( printf("window.c: setupWindow() WindowToFront()\n") );
  155.         
  156.       WindowToFront( window );
  157.       return;      /* already setup, nothing to re-init  */
  158.    }
  159.    if( ! myscreen)
  160.    {
  161.       if( ! (myscreen=LockPubScreen(NULL)))
  162.       {
  163.          D1( printf("window.c: setupWindow() could not LockPubScreen()\n") );
  164.          return;
  165.       }
  166.    }
  167.    D1( printf("window.c: setupWindow() LockPubScreen(NULL) = %lx\n",myscreen) );
  168.  
  169.    if( ! mydi)
  170.    {
  171.       if( ! (mydi=GetScreenDrawInfo(myscreen)))
  172.       {
  173.          D1( printf("window.c: setupWindow() could not GetScreenDrawInfo()\n") );
  174.          return;
  175.       }
  176.    }
  177.    D1( printf("window.c: setupWindow() GetScreenDrawInfo(0x%lx) = %lx\n",myscreen,mydi) );
  178.  
  179.   topborder=myscreen->WBorTop + (myscreen->Font->ta_YSize +1);
  180.   
  181.    D1( printf("window.c: setupWindow() topborder = %ld\n",topborder) );
  182.  
  183.    if( ! vi)
  184.    {
  185.       if( ! (vi=GetVisualInfo(myscreen,TAG_DONE)))
  186.       {
  187.          D1( printf("window.c: setupWindow() could not GetVisualInfo()\n") );
  188.          goto EXIT;
  189.       }
  190.    }
  191.    D1( printf("window.c: setupWindow() GetVisualInfo() = %lx\n",vi) );
  192.     
  193.      
  194.     /******** Use ToolTypes if Available **********/
  195.        if (windowfirstopening == TRUE )
  196.           {
  197.            if ( ! (window=getFirstWindow()))
  198.    {
  199.       D1( printf("window.c: setupWindow() could not getNewWindow()\n") );
  200.       goto EXIT;
  201.    }
  202.    D1( printf("window.c: setupWindow() getNewWindow() = %lx\n",window) );
  203.                           windowfirstopening = FALSE;
  204.           }
  205.           else
  206.  
  207.     /**************************/
  208.    if ( ! (window=getNewWindow()))
  209.    {
  210.       D1( printf("window.c: setupWindow() could not getNewWindow()\n") );
  211.       goto EXIT;
  212.    }
  213.    D1( printf("window.c: setupWindow() getNewWindow() = %lx\n",window) );
  214.  
  215.     /**********************/
  216.  
  217.    iport    = window->UserPort;
  218.    isigflag = 1L << iport->mp_SigBit;
  219.  
  220.    addGadgets(window);
  221.  
  222.    setupCustomMenu();
  223.    if(menu)
  224.    {
  225.       if( ! LayoutMenus(menu,vi,
  226.             GTMN_NewLookMenus, TRUE,
  227.             TAG_DONE))
  228.  
  229.       {
  230.          D1( printf("window.c: setupWindow() could not LayoutMenus()\n") );
  231.       }
  232.    }
  233.    menuattached=SetMenuStrip(window,menu);
  234.    D1( printf("window.c: setupWindow() SetMenuStrip() = %lx\n",menuattached) );
  235.  
  236.    refreshWindow();
  237.  
  238. EXIT: ;
  239. }
  240. /****i* Blank.ld/shutdownWindow() ******************************************
  241. *
  242. *   NAME
  243. *        shutdownWindow -- Perform the steps necessary to close the window.
  244. *
  245. *   SYNOPSIS
  246. *        shutdownWindow()
  247. *
  248. *        VOID shutdownWindow(VOID);
  249. *
  250. *   FUNCTION
  251. *        Closes the window and remembers its position for the next open.
  252. *
  253. *   INPUTS
  254. *
  255. *   RESULT
  256. *
  257. *   EXAMPLE
  258. *
  259. *   NOTES
  260. *
  261. *   BUGS
  262. *
  263. *   SEE ALSO
  264. *
  265. *****************************************************************************
  266. *
  267. */
  268. VOID shutdownWindow()
  269. {
  270.    WORD   *wp;
  271.  
  272.    D1( printf("window.c: shutdownWindow() enter\n") );
  273.  
  274.    if ( ! window )
  275.    {
  276.       D1( printf("window.c: shutdownWindow() window not open!\n") );
  277.       return;
  278.    }
  279.  
  280.    /* save window position   */
  281.     wp   = savewindow;
  282.    *wp++ = window->LeftEdge;
  283.    *wp++ = window->TopEdge;
  284.    *wp++ = window->Width;
  285.    *wp   = window->Height;
  286.  
  287.    if(menuattached)
  288.    {
  289.       ClearMenuStrip(window);
  290.       menuattached=NULL;
  291.    }
  292.    if(menu)
  293.    {
  294.       FreeMenus(menu);
  295.       menu=NULL;
  296.    }
  297.  
  298.    D1( printf("window.c: shutdownWindow() ClosingWindow(%lx)\n",window) );
  299.    CloseWindow(window);
  300.    D1( printf("window.c: shutdownWindow() after CloseWindow()\n") );
  301.    window   = NULL;
  302.    iport    = NULL;
  303.    isigflag = NULL;
  304.  
  305.    removeGadgets();
  306.    D1( printf("window.c: shutdownWindow() after removeGadgets()\n") );
  307.  
  308.    if(vi)
  309.    {
  310.       D1( printf("window.c: shutdownWindow() FreeVisualInfo(%lx)\n",vi) );
  311.       FreeVisualInfo(vi);
  312.       vi=NULL;
  313.    }
  314.    if(mydi)
  315.    {
  316.       D1( printf("window.c: shutdownWindow() FreeScreenDrawInfo(%lx)\n",mydi) );
  317.       FreeScreenDrawInfo(myscreen,mydi);
  318.       mydi=NULL;
  319.    }
  320.    if(myscreen)
  321.    {
  322.       D1( printf("window.c: shutdownWindow() UnlockPubScreen(%lx)\n",myscreen) );
  323.       UnlockPubScreen(NULL,myscreen);
  324.       myscreen=NULL;
  325.    }
  326. }
  327.  
  328. /****i* Blank.ld/getNewWindow() ******************************************
  329. *
  330. *   NAME
  331. *        getNewWindow -- Open the window remembering the old postition
  332. *                        if reopening.
  333. *
  334. *   SYNOPSIS
  335. *        window = getNewWindow();
  336. *
  337. *        struct Window *getNewWindow(VOID);
  338. *
  339. *   FUNCTION
  340. *        This function opens the commodities window. It automatically
  341. *        sets the window title to reflect the current HotKey. If the
  342. *        window has already been opened once then the previous position
  343. *        and size (if sizing is enabled) are used for this open.
  344. *
  345. *   INPUTS
  346. *        None.
  347. *
  348. *   RESULT
  349. *        Returns a pointer to the opened window or NULL on error.
  350. *
  351. *   EXAMPLE
  352. *
  353. *   NOTES
  354. *
  355. *   BUGS
  356. *
  357. *   SEE ALSO
  358. *        setupWindow();
  359. *        shutdownWindow();
  360. *
  361. *****************************************************************************
  362. *
  363. */
  364. struct  Window *getNewWindow()
  365. {
  366.    struct  NewWindow   nw;
  367.    WORD   *wp = savewindow;
  368.    D1( printf("window.c: getNewWindow() enter\n") );
  369.  
  370.    sprintf(WindowTitle,"%s:Hotkey = %s",COM_TITLE,hotkeybuff);
  371.  
  372.    nw.LeftEdge    = *wp++;
  373.    nw.TopEdge     = *wp++;
  374.    nw.Width       = *wp++;  
  375.    nw.Height      = *wp++;
  376.    nw.DetailPen   = (UBYTE) -1;
  377.    nw.BlockPen    = (UBYTE) -1;
  378.    nw.IDCMPFlags  = IFLAGS;
  379.    nw.Flags       = WFLAGS;
  380.    nw.FirstGadget = NULL;
  381.    nw.CheckMark   = NULL;
  382.    nw.Title       = WindowTitle;
  383.    nw.Screen      = myscreen;   
  384.    nw.BitMap      = NULL;
  385.    nw.MinWidth    = WINDOW_MIN_WIDTH;
  386.    nw.MinHeight   = WINDOW_MIN_HEIGHT;
  387.    /* work around bug  */
  388.    nw.MaxWidth    = WINDOW_MAX_WIDTH;
  389.    nw.MaxHeight   = WINDOW_MAX_HEIGHT;
  390.    nw.Type        = PUBLICSCREEN;
  391.  
  392.   
  393.    D1( printf("window.c: getNewWindow() before OpenWindowTags()\n") );
  394.      
  395.   
  396.                                    /**************************/
  397.  
  398.  
  399.  
  400.    return ((struct Window *) OpenWindowTags( &nw,
  401.                                       /*   WA_Left,WINDOW_LEFT,     */
  402.                                       /*   WA_Top,WINDOW_TOP,       */
  403.                                       /*   WA_Width,WINDOW_WIDTH,   */
  404.                                       /*   WA_Height,WINDOW_HEIGHT, */
  405.                                            WA_AutoAdjust,TRUE,
  406.                              CYCLEIDCMP,
  407.                               WA_Gadgets,  
  408.                               WA_NewLookMenus, TRUE,
  409.                               GTMN_NewLookMenus, TRUE,
  410.                               WA_PubScreenName, (LONG) start_on_publicscreen,
  411.                               WA_PubScreenFallBack, TRUE,
  412.                               TAG_DONE));
  413. }
  414.  
  415.  
  416.  
  417.  
  418. /**********************************************************/
  419. struct  Window *getFirstWindow()
  420. {
  421.    struct  NewWindow   nw;
  422.    WORD   *wp = savewindow;
  423.    D1( printf("window.c: getNewWindow() enter\n") );
  424.  
  425.    sprintf(WindowTitle,"%s:Hotkey = %s",COM_TITLE,hotkeybuff);
  426.  
  427.    nw.LeftEdge    = *wp++;
  428.    nw.TopEdge     = *wp++;
  429.    nw.Width       = *wp++;
  430.    nw.Height      = *wp++;
  431.    nw.DetailPen   = (UBYTE) -1;
  432.    nw.BlockPen    = (UBYTE) -1;
  433.    nw.IDCMPFlags  = IFLAGS;
  434.    nw.Flags       = WFLAGS;
  435.    nw.FirstGadget = NULL;
  436.    nw.CheckMark   = NULL;
  437.    nw.Title       = WindowTitle;
  438.    nw.Screen      = myscreen;
  439.    nw.BitMap      = NULL;
  440.    nw.MinWidth    = WINDOW_MIN_WIDTH;
  441.    nw.MinHeight   = WINDOW_MIN_HEIGHT;
  442.    /* work around bug  */
  443.    nw.MaxWidth    = WINDOW_MAX_WIDTH;
  444.    nw.MaxHeight   = WINDOW_MAX_HEIGHT;
  445.    nw.Type        = PUBLICSCREEN;
  446.  
  447.    D1( printf("window.c: getNewWindow() before OpenWindowTags()\n") );
  448.  
  449.    return ((struct Window *) OpenWindowTags( &nw,
  450.                                            WA_Left, Windowx,
  451.                                            WA_Top,Windowy,
  452.                                       /*   WA_Width,WINDOW_WIDTH,   */
  453.                                       /*   WA_Height,WINDOW_HEIGHT, */
  454.                                            WA_AutoAdjust,TRUE,
  455.                              CYCLEIDCMP,
  456.                               WA_Gadgets,
  457.                               WA_NewLookMenus, TRUE,
  458.                               GTMN_NewLookMenus, TRUE,
  459.                               WA_PubScreenName, (LONG) start_on_publicscreen,
  460.                               WA_PubScreenFallBack, TRUE,
  461.                               TAG_DONE));
  462. }
  463.  
  464.  
  465. /****i* Blank.ld/addGadgets() ******************************************
  466. *
  467. *   NAME
  468. *        addGadgets -- Add all the standard and application specific
  469. *                      gadgets to the window.
  470. *
  471. *   SYNOPSIS
  472. *        result = addGadgets(window);
  473. *
  474. *        int addGadgets(struct Window *window);
  475. *
  476. *   FUNCTION
  477. *        Sets up the environment for gadget toolkit gadgets and calls
  478. *        addCustomGadgets() to add the application specific gadgets
  479. *        to the window.
  480. *
  481. *   INPUTS
  482. *        window = Pointer to the window.
  483. *
  484. *   RESULT
  485. *        Returns TRUE if all went well, FALSE otherwise.
  486. *
  487. *   EXAMPLE
  488. *
  489. *   NOTES
  490. *
  491. *   BUGS
  492. *
  493. *   SEE ALSO
  494. *        setupCustomGadgets();
  495. *        removeGadgets();
  496. *
  497. *****************************************************************************
  498. *
  499. */
  500. int addGadgets(struct Window *window)
  501. {
  502.    struct Gadget *gad;
  503.  
  504.    D1( printf("window.c: addGadgets() enter\n") );
  505.   /*******************************/
  506.    {
  507.     LONG longval;
  508.  
  509.         customtattr.ta_Style = 0;
  510.         customtattr.ta_Flags = 0;
  511.         customtattr.ta_Name = fontname;
  512.         stcd_l(fontsize, &longval);
  513.         customtattr.ta_YSize = longval;
  514.         mydesiredfont = &customtattr;
  515.      }
  516.  
  517.     
  518.    
  519.    /*  Open desired font: */
  520.    if( ! customfont )
  521.    {
  522.       D1( printf("window.c: addGadgets() Opening font\n") );
  523.       if (!(customfont = OpenDiskFont(mydesiredfont)))
  524.       {
  525.  
  526.         ErrorF ("Could not open font %s %ld\n",
  527.                 customtattr.ta_Name,
  528.                 customtattr.ta_YSize);
  529.            terminate();
  530.       }
  531.    }
  532.  
  533.    gad = CreateContext(&glist);
  534.  
  535.    setupCustomGadgets(&gad);
  536.   
  537.    if(!gad)
  538.    {
  539.       D1( printf("window.c: addGadgets() error adding gadgets\n") );
  540.       if(glist)
  541.       {
  542.          FreeGadgets(glist);
  543.          glist=NULL;
  544.       }
  545.  
  546.     
  547.       if(customfont)
  548.       {
  549.          CloseFont(customfont);
  550.          customfont=NULL;
  551.       }
  552.       return(FALSE);
  553.    }
  554.      
  555.    AddGList(window, glist, ((UWORD) -1), ((UWORD) -1), NULL);
  556.    RefreshGList(window->FirstGadget, window, NULL, ((UWORD) -1));
  557.    GT_RefreshWindow(window,NULL);
  558.    return(TRUE);
  559. }
  560. /****i* Blank.ld/removeGadgets() ******************************************
  561. *
  562. *   NAME
  563. *        removeGadgets -- Remove and deallocate all standard and
  564. *                         application gadgets from the window.
  565. *
  566. *   SYNOPSIS
  567. *        removeGadgets()
  568. *
  569. *        VOID removeGadgets(VOID);
  570. *
  571. *   FUNCTION
  572. *        This function performs gadget cleanup. It is responsible for
  573. *        deallocating and removing all gadgets from the window before
  574. *        it is closed.
  575. *
  576. *   INPUTS
  577. *        None.
  578. *
  579. *   RESULT
  580. *        All gadgets are freed and removed from the window.
  581. *
  582. *   EXAMPLE
  583. *
  584. *   NOTES
  585. *        Uses the global variable glist which is a linked list off all
  586. *        the gadget toolkit gadgets.
  587. *
  588. *   BUGS
  589. *
  590. *   SEE ALSO
  591. *
  592. *****************************************************************************
  593. *
  594. */
  595. void removeGadgets()
  596. {
  597.    if(glist)
  598.    {
  599.       D1( kprintf("window.c: removeGadgets() FreeingGadgets(%lx)\n",glist) );
  600.       FreeGadgets(glist);
  601.       glist=NULL;
  602.    }
  603.  
  604.    if (customfont)
  605.    {
  606.       D1( kprintf("window.c: removeGadgets() Closing font %lx\n", font) );
  607.       CloseFont(customfont);
  608.       customfont=NULL;
  609.      
  610.    }
  611. }
  612. #endif /* WINDOW */
  613.